package cn.wit.summit.chwan.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.CodeloginRespone;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.MobileCodeUtils;
import cn.wit.summit.chwan.utils.NetWorkUtils;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.utils.ToastUtils;
import cn.wit.summit.chwan.widgets.RecommendTopDownloadV2View;
import cn.wit.summit.chwan.widgets.RecommendTopEnergyView;
import cn.wit.summit.chwan.widgets.RecommendTopWalletV2View;
import retrofit2.Retrofit;

public class ChangePwdActivity extends MyActivity {

    @Bind(R.id.tv_Verification)
    TextView tv_Verification;
    @Bind(R.id.tv_showtime)
    TextView tv_showtime;
    @Bind(R.id.ed_iphone)
    EditText ed_code;
    @Bind(R.id.ed_new_pwd)
    EditText ed_pwd;
    @Bind(R.id.btn_result)
    Button btn_result;
    int index = 60;

    @Bind(R.id.tv_iphone_number)
    TextView tv_iphone_number;
    boolean isCode = false;
    boolean isPwd = false;
    private String Ipnone;

    private RetrofitManager retrofitManager;
    private Retrofit retrofitLogin;
    private ApiUtil apiLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.bind(this);

        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofitLogin = retrofitManager.getLoginRetrofit();
        apiLogin = retrofitLogin.create(ApiUtil.class);

        setBarTitleTx(getString(R.string.tv_title));
        tv_iphone_number.setText(MyApplication.accountBean.getMobile());
        Ipnone = MyApplication.accountBean.getMobile();
        if (StringUtils.isNotEmpty(tv_iphone_number.getText().toString().trim())) {
            tv_Verification.setTextColor(getResources().getColor(R.color.color_txt_blue));
        } else {
            tv_Verification.setTextColor(getResources().getColor(R.color.home_gray));
        }
    }

    @OnClick({R.id.tv_Verification, R.id.btn_result, R.id.ed_new_pwd, R.id.ed_iphone})
    public void onViewOnclick(View view) {
        switch (view.getId()) {
            case R.id.tv_Verification:
                //点击获取验证码
                if (NetWorkUtils.isNetworkConnected(ChangePwdActivity.this))
                    getVerification();
                else
                    ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem("网络连接失败，再试试吧~");
                break;
            case R.id.btn_result:
                //确认按钮
                getSubmit();
                break;
            case R.id.ed_new_pwd:
                ed_pwd.setFocusableInTouchMode(true);
                ed_pwd.setFocusable(true);
                ed_pwd.requestFocus();
                break;
            case R.id.ed_iphone:
                ed_code.setFocusableInTouchMode(true);
                ed_code.setFocusable(true);
                ed_code.requestFocus();
                break;
        }
    }

    /**
     * 确认密码
     */
    private void getSubmit() {
        String pwd = ed_pwd.getText().toString().trim();
        String Code = ed_code.getText().toString().trim();
        if (StringUtils.isNotEmpty(pwd) && StringUtils.isNotEmpty(Code)) {
            if (pwd.length() > 5 && pwd.length() < 21) {
//                if (RegularUtil.getPwd(pwd)==true)
                getNetChangePwd(pwd, Code);
            } else {
                ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem("请设置6到20位长度密码");
            }

        } else {
            ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem("密码或验证码不能为空");
        }

    }

    private void getNetChangePwd(String pwd, String code) {
        int uid = AccountUtil.getInstance().getUid();
        String token = AccountUtil.getInstance().getToken();
        if (token == null) {
            ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem("token为空");
            return;
        }
        if (NetWorkUtils.isNetworkConnected(ChangePwdActivity.this)) {
            apiLogin.getNewPwd(pwd, code, uid, token).enqueue(new MyCallback<ResponseBase<CodeloginRespone>>() {
                @Override
                public void onSuccess(ResponseBase<CodeloginRespone> model) {
                    ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem("修改密码成功");
                    finish();
                }

                @Override
                public void onFailure(int code, String msg) {
                    ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem(msg);
                }
            });
        } else {
            ToastUtils.getInstance(ChangePwdActivity.this).showToastSystem("网络连接失败，再试试吧~");
        }
    }

    /**
     * 获取验证码
     */
    private void getVerification() {
        tv_Verification.setVisibility(View.GONE);
        tv_showtime.setVisibility(View.VISIBLE);
        index = 60;
        ed_code.setFocusable(true);
        ed_code.setFocusableInTouchMode(true);
        ed_code.requestFocus();
        InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        m.toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);
//       getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//       showSoftInputFromWindow(ChangePwdActivity.this,ed_code);
        tv_showtime.setText(60 + "s后重发");
        setLoadTime();
        MobileCodeUtils.getChangePWDCode(ChangePwdActivity.this, Ipnone);
    }


    private void setLoadTime() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                --index;
                tv_showtime.setText("" + index + "s后重发");
                if (index == 0) {
                    tv_showtime.setVisibility(View.GONE);
                    tv_Verification.setVisibility(View.VISIBLE);
                } else {
                    setLoadTime();
                }
            }
        }, 1000);
    }

}
